home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / general / cdbw.exe / SCLIENT.C < prev    next >
Text File  |  1991-08-28  |  20KB  |  669 lines

  1. /*
  2.  *  SCLIENT.C
  3.  *
  4.  *  This module contains functions associated with the client dialog box
  5.  *  in SAMPLE.EXE.
  6.  *
  7.  *  Copyright (C) 1991 by Daytris.  All rights reserved.
  8.  */
  9.  
  10. #include <windows.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <math.h>
  15. #ifndef ZORTECH
  16. #include <memory.h>
  17. #endif
  18. #include "dbmgr.h"
  19. #include "sampledb.h"
  20. #include "sample.h"
  21.  
  22.  
  23. /************************************************
  24.  * Local data
  25.  ************************************************/
  26.  
  27. static CLIENT client;
  28. static enum 
  29.     {
  30.     MODE_ADD,
  31.     MODE_UPDATE,
  32.     MODE_DELETE
  33.     } eMode;
  34. static BOOL bAddressChange;
  35. static HANDLE hAddresses;
  36.  
  37.  
  38. /************************************************
  39.  * Function Declarations
  40.  ************************************************/
  41.  
  42. BOOL AddClientDlg( HWND hWnd);
  43. BOOL UpdateClientDlg( HWND hWnd);
  44. BOOL DeleteClientDlg( HWND hWnd);
  45. static BOOL GetSelectedClient( HWND hWnd, CLIENT *pClient, short *pIndexSel);
  46. static BOOL AddClientAddresses( HWND hWnd);
  47. static BOOL DeleteClientAddresses( HWND hWnd);
  48. static BOOL FreeClientAddresses( HWND hWnd);
  49. BOOL FAR PASCAL ClientProc( HWND hDlg, unsigned iMessage, WORD wParam,
  50.     LONG lParam);
  51. static void SetClientFields( HWND hDlg);
  52. static BOOL GetClientFields( HWND hDlg);
  53. static BOOL StoreAddressHandles( HWND hDlg);
  54.  
  55.  
  56. /***************************************************************************
  57.  * Function : AddClientDlg
  58.  *
  59.  * Purpose  : This function drives the add client dialog box.
  60.  *
  61.  * Returns  : TRUE  - client added
  62.  *            FALSE - add aborted or error in add
  63.  ***************************************************************************/
  64. BOOL AddClientDlg( HANDLE hWnd)
  65.     {
  66.     short nStatus;
  67.     DWORD dwStatus;
  68.     FARPROC lpfnClientProc;
  69.  
  70.     /* Set static variables */
  71.     eMode = MODE_ADD;
  72.     bAddressChange = FALSE;
  73.  
  74.     /* Initialize the client structure */
  75.     memset( &client, 0, sizeof( CLIENT));
  76.     client.lClientNbr = setup.lNextClientNbr;
  77.  
  78.     /* Create an instance and open the Client window */
  79.     lpfnClientProc = MakeProcInstance( ClientProc, hInst);
  80.     nStatus = DialogBox( hInst, "client", hWnd, lpfnClientProc);
  81.     FreeProcInstance( lpfnClientProc);
  82.  
  83.     /* User selected OK */
  84.     if( nStatus == IDOK)
  85.         {
  86.         /* Add the client */
  87.         if( dwStatus = XDbRecordAdd( hDb, "client", &client,
  88.             sizeof( CLIENT)) )
  89.             {
  90.             DbError( hWnd, dwStatus, __FILE__, __LINE__);
  91.             return FALSE;
  92.             }
  93.  
  94.         /* Add the member addresses */
  95.         if( bAddressChange)
  96.             {
  97.             if( ! AddClientAddresses( hWnd))
  98.                 return FALSE;
  99.             }
  100.  
  101.         /* Increment the client number counter and update the setup
  102.            record */
  103.         setup.lNextClientNbr++;
  104.         if( dwStatus = XDbRecordUpdate( hDb, "setup", &setup,
  105.             sizeof( SETUP)))
  106.             {
  107.             DbError( hWnd, dwStatus, __FILE__, __LINE__);
  108.             return FALSE;
  109.             }
  110.  
  111.         /* Flush the database */
  112.         DbFlush( hDb);
  113.  
  114.         /* Add the client to the list box */
  115.         AddToClientListBox( hWnd, &client);
  116.         }
  117.     else    /* IDCANCEL */
  118.         {
  119.         /* Free all address handles for this client */
  120.         if( ! FreeClientAddresses( hWnd))
  121.             return FALSE;
  122.         }
  123.  
  124.     return TRUE;
  125.     }
  126.  
  127.  
  128. /***************************************************************************
  129.  * Function : UpdateClientDlg
  130.  *
  131.  * Purpose  : This function drives the add client dialog box.
  132.  *
  133.  * Returns  : TRUE  - client added
  134.  *            FALSE - update aborted or error in update
  135.  ***************************************************************************/
  136. BOOL UpdateClientDlg( HANDLE hWnd)
  137.     {
  138.     short nStatus, nIndex;
  139.     DWORD dwStatus;
  140.     FARPROC lpfnClientProc;
  141.  
  142.     /* Set the mode */
  143.     eMode = MODE_UPDATE;
  144.  
  145.     /* Initialize the client structure */
  146.     if( ! GetSelectedClient( hWnd, &client, &nIndex))
  147.         {
  148.         MessageBeep( 0);
  149.         return FALSE;
  150.         }
  151.  
  152.     /* Create an instance and open the Client window */
  153.     lpfnClientProc = MakeProcInstance( ClientProc, hInst);
  154.     nStatus = DialogBox( hInst, "client", hWnd, lpfnClientProc);
  155.     FreeProcInstance( lpfnClientProc);
  156.  
  157.     /* User selected OK */
  158.     if( nStatus == IDOK)
  159.         {
  160.         /* Update the client */
  161.         if( dwStatus = XDbRecordUpdate( hDb, "client", &client,
  162.             sizeof( CLIENT)) )
  163.             {
  164.             DbError( hWnd, dwStatus, __FILE__, __LINE__);
  165.             return FALSE;
  166.             }
  167.  
  168.         /* If any address has changed, delete all member addresses and
  169.            add addresses in handle table. */
  170.         if( bAddressChange)
  171.             {
  172.             if( ! DeleteClientAddresses( hWnd))
  173.                 return FALSE;
  174.             if( ! AddClientAddresses( hWnd))
  175.                 return FALSE;
  176.             }
  177.  
  178.         /* Flush the database */
  179.         DbFlush( hDb);
  180.  
  181.         /* Update the listbox */
  182.         DeleteFromClientListBox( hWnd, nIndex);
  183.         AddToClientListBox( hWnd, &client);
  184.         }
  185.     else    /* IDCANCEL */
  186.         {
  187.         /* Free all address handles for this client */
  188.         if( ! FreeClientAddresses( hWnd))
  189.             return FALSE;
  190.         }
  191.  
  192.     return TRUE;
  193.     }
  194.  
  195.  
  196. /***************************************************************************
  197.  * Function : DeleteClientDlg
  198.  *
  199.  * Purpose  : This function drives the client record deletion process.
  200.  *
  201.  * Returns  : TRUE  - client deleted
  202.  *            FALSE - delete aborted or error in delete
  203.  ***************************************************************************/
  204. BOOL DeleteClientDlg( HWND hWnd)
  205.     {
  206.     short nStatus, nIndex;
  207.     DWORD dwStatus;
  208.  
  209.     /* Initialize the client structure */
  210.     if( ! GetSelectedClient( hWnd, &client, &nIndex))
  211.         {
  212.         MessageBeep( 0);
  213.         return FALSE;
  214.         }
  215.  
  216.     /* Ask user if they're sure */
  217.     nStatus = MessageBox( hWnd, "Are you sure?", "Delete Client",
  218.         MB_ICONQUESTION | MB_YESNO);
  219.  
  220.     /* User selected YES */
  221.     if( nStatus == IDYES)
  222.         {
  223.         /* Delete all member addresses */
  224.         if( ! DeleteClientAddresses( hWnd))
  225.             return FALSE;
  226.  
  227.         /* Delete the client */
  228.         if( dwStatus = DbRecordDelete( hDb, "client"))
  229.             {
  230.             DbError( hWnd, dwStatus, __FILE__, __LINE__);
  231.             return FALSE;
  232.             }
  233.  
  234.         /* Flush the database */
  235.         DbFlush( hDb);
  236.  
  237.         /* Remove client from listbox */
  238.         if( ! DeleteFromClientListBox( hWnd, nIndex))
  239.             return FALSE;
  240.         }
  241.  
  242.     return TRUE;
  243.     }
  244.  
  245.  
  246. /***************************************************************************
  247.  * Function : GetSelectedClient
  248.  *
  249.  * Purpose  : This function retrieves the selected client record from
  250.  *            the database.  It uses the client number in the string
  251.  *            of the listbox as a key value to retrieve upon.  The
  252.  *            listbox index is also returned.
  253.  *
  254.  * Returns  : TRUE  - client retrieved
  255.  *            FALSE - error
  256.  ***************************************************************************/
  257. static BOOL GetSelectedClient( HWND hWnd, CLIENT *pClient, short *pIndex)
  258.     {
  259.     LONG lKey;
  260.     DWORD dwStatus;
  261.     char szBuffer[64], *pTemp;
  262.  
  263.     /* Get the listbox selection */
  264.     if( (*pIndex = (short)SendMessage( hWndClientLB, LB_GETCURSEL, 0, 0L))
  265.         == LB_ERR)
  266.         {
  267.         return FALSE;
  268.         }
  269.     if( SendMessage( hWndClientLB, LB_GETTEXT, *pIndex,
  270.         (LONG)(LPSTR)szBuffer) == LB_ERR)
  271.         {
  272.         MessageBox( hWnd, "SendMessage / LB_GETTEXT", "Fatal Error",
  273.             MB_ICONEXCLAMATION | MB_OK);
  274.         return FALSE;
  275.         }
  276.  
  277.     /* Find the client number in the selected string */
  278.     if( bSortByNumber)
  279.         pTemp = strtok( szBuffer, " ");
  280.     else
  281.         {
  282.         pTemp = strchr( szBuffer, 0);
  283.         for( pTemp-- ; *pTemp != ' ' ; pTemp--)
  284.             ;
  285.         pTem